package com.tuyan.aixiang.common;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.gyf.immersionbar.ImmersionBar;
import com.tuyan.aixiang.activity.web.WebActivity;
import com.tuyan.aixiang.common.lang.VoidFunction;
import com.tuyan.aixiang.util.StringUtil;

import androidx.fragment.app.Fragment;
import butterknife.ButterKnife;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;
//import pub.devrel.easypermissions.AfterPermissionGranted;
//import pub.devrel.easypermissions.EasyPermissions;

public abstract class BaseFragment<P extends BasePresenter> extends Fragment implements IFragment {


    CompositeDisposable compositeDisposable = new CompositeDisposable();

    protected P iPresenter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        int layoutId = getLayoutId();
        View contentView = inflater.inflate(layoutId, container, false);
        ButterKnife.bind(this, contentView);
        iPresenter = createPresenter();
        if(null != iPresenter) {
            iPresenter.register(this);
        }
        initView(contentView);
        return contentView;
    }


    @Override
    public void unBindView() {
        if(null != iPresenter) {
            iPresenter.unRegister();
        }
    }

    protected Integer getStatusBarOffset() {
        if (ImmersionBar.hasNotchScreen(this)) {
            return ImmersionBar.getNotchHeight(this);
        } else {

            return ImmersionBar.getStatusBarHeight(getActivity());
        }
    }

    @Override
    public void addDisposable(Disposable disposable) {
        compositeDisposable.add(disposable);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        compositeDisposable.dispose();
        unBindView();
    }

    @Override
    public void openWeb(String link) {
        Intent intent = new Intent(getActivity(), WebActivity.class);
        intent.putExtra("url", link);
        startActivity(intent);
    }

    protected abstract void initView(View contentView);

    protected abstract int getLayoutId();

    @Override
    public void loadImage(String uuid, ImageView iv) {
//        Disposable disposable = HttpClient.getPub().res(uuid).compose(RxSchedulers.io_main())
//                .compose(ResultTransformer.handleResult())
//                .subscribe(r -> {
//                    if (null == r.get()) {
//                        return;
//                    }
        if (StringUtil.isBlank(uuid)) return;
        Glide.with(this).load(Constant.getResUrl(uuid)).diskCacheStrategy(DiskCacheStrategy.ALL).into(iv);
//
//                }, t -> {
//                });
//        addDisposable(disposable);
    }


//    @AfterPermissionGranted(3403)
//    public boolean taskContactsRead(VoidFunction function) {
//        String[] perms = {Manifest.permission.READ_CONTACTS};
//        if (EasyPermissions.hasPermissions(getContext(), perms)) {
////            taskContactsReadCallback();
//            function.call();
//            return true;
//        } else {
//            EasyPermissions.requestPermissions(this, "请求通讯录权限", 3303, perms);
//            return false;
//        }
//    }

    protected abstract P createPresenter();

    public void showLoading() {

    }

    public void hideLoading() {

    }

}
